﻿using Mono.Data.Sqlite;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class NewExamMgr : MonoBehaviour {
    public static NewExamMgr s_Instance;
    public string m_EditorName;
    public Transform m_ListContent;
    public List<OneExam> m_OneExamList = new List<OneExam>();
    public OneExam m_OneExamPrefab;
    public InputField m_ExamNameInput, m_ExamStemInput;
    public Text m_EditorText;

    private OneExam m_oneExamNow;
	// Use this for initialization
	void Awake () {
        s_Instance = this;

    }

    private void Start() {
        m_EditorName = PlayerPrefs.GetString("EditorName", "555");
        Init();
    }

    void Init() {
        m_OneExamList.Clear();
        for (int i = 0;i<m_ListContent.childCount;i++)
        {
            Destroy(m_ListContent.GetChild(i).gameObject);
        }

        string cmd = "select * from PAPER_TABLE";
        SqliteDataReader reader = SQLManager.s_Instance.ExecuteSQLCommand(cmd);
        if (reader != null) {
            DataTable dataTable = new DataTable();
            dataTable.Load(reader);
            if (dataTable.Rows.Count > 0) {
                for (int i = 0; i < dataTable.Rows.Count; i++) {
                    string name = dataTable.Rows[i]["Name"].ToString();
                    string editor = dataTable.Rows[i]["Editor"].ToString();
                    OneExam exam = Instantiate(m_OneExamPrefab) as OneExam;
                    exam.transform.SetParent(m_ListContent);
                    exam.transform.localScale = new Vector3(1f,1f,1f);
                    exam.Init(name, editor);
                    m_OneExamList.Add(exam);
                }
            }
        }
    }
    public void OnOneExamClick(OneExam exam) {
        foreach (var item in m_OneExamList)
        {
            item.SetState(false);
        }
        m_oneExamNow = exam;
        exam.SetState(true);
        string cmd = "select * from PAPER_TABLE WHERE Name ='" + exam.m_TitleText.text + "'";
        SqliteDataReader reader = SQLManager.s_Instance.ExecuteSQLCommand(cmd);
        if (reader != null) {
            DataTable dataTable = new DataTable();
            dataTable.Load(reader);
            if (dataTable.Rows.Count > 0) {
                string name = dataTable.Rows[0]["Name"].ToString();
                string stem = dataTable.Rows[0]["Stem"].ToString();
                string editor = dataTable.Rows[0]["Editor"].ToString();
                m_ExamNameInput.text = name;
                m_ExamStemInput.text = stem;
                m_EditorText.text = editor;
            }
        }
    }
    public void OnClickAddExam() {
        if (m_ExamNameInput.text.Length > 0) {
            string cmd = "select * from PAPER_TABLE where Name = '" + m_ExamNameInput.text+"'";
            SqliteDataReader reader = SQLManager.s_Instance.ExecuteSQLCommand(cmd);
            if (reader != null) {
                DataTable dataTable = new DataTable();
                dataTable.Load(reader);
                if (dataTable.Rows.Count > 0) {//已存在试卷
                    string cmd1 = "UPDATE PAPER_TABLE SET " +
                        "Stem = '" + m_ExamStemInput.text + "'," +
                        "Editor = '" + m_EditorName + "' " +
                        "WHERE Name = '" + m_ExamNameInput.text + "'";
                    SQLManager.s_Instance.ExecuteSQLCommand(cmd1);
                } else {//不存在试卷
                        string cmd2 =
                        "INSERT INTO PAPER_TABLE ('" +
                        "Stem"+"','"+
                        "Editor" + "','" +
                        "Name" + "') " +
                        "VALUES ('" +
                        m_ExamStemInput.text + "','" +
                        m_EditorName + "','" +
                        m_ExamNameInput.text + "')";
                        SQLManager.s_Instance.ExecuteSQLCommand(cmd2);
                }
            }
        }
        Init();
    }
    public void OnClickDelete() {
        if (m_oneExamNow != null) {
            string cmd = "delete from PAPER_TABLE where Name = '" + m_oneExamNow.m_TitleText.text + "'";
            SQLManager.s_Instance.ExecuteSQLCommand(cmd);
        }
        Init();
    }
    public void OnClickEditExam() {
        if (m_oneExamNow != null) {
            string cmd = "select * from PAPER_TABLE where Name = '" + m_ExamNameInput.text + "'";
            SqliteDataReader reader = SQLManager.s_Instance.ExecuteSQLCommand(cmd);
            if (reader != null) {
                DataTable dataTable = new DataTable();
                dataTable.Load(reader);
                if (dataTable.Rows.Count > 0) {
                    long examID = (long)dataTable.Rows[0]["ID"];
                    PlayerPrefs.SetInt("ExamID", (int)examID);
                }
            }
                
            SceneManager.LoadScene("SetQuestion");
        }
    }
	public void OnClickHomeButton() {
        SceneManager.LoadScene("Scene");
    }
}
